package com.laoniu.biz;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.laoniu.dto.PageResponse;
import com.laoniu.dto.request.LogRequestDto;
import com.laoniu.dto.response.LogResponse;
import com.laoniu.entity.SysLog;
import com.laoniu.service.sys.LogService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogBiz {

    @Autowired
    private LogService logService;

    public PageResponse<LogResponse> list(LogRequestDto requestDto){
        Page<SysLog> page = logService.pageList(requestDto);
        PageResponse<LogResponse> result = new PageResponse<LogResponse>();
        result.setPageNo(requestDto.getPageNo());
        result.setPageSize(requestDto.getPageSize());
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            List<LogResponse> list = Lists.newArrayList();
            for (SysLog record : page.getRecords()) {
                LogResponse response = Convert.convert(LogResponse.class, record);
                list.add(response);
            }
            result.setData(list);
        }
        result.setTotal(page.getTotal());
        return result;
    }
}
